package com.example.demo.controller;

import com.example.demo.util.HttpClientUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;

/**
 * @author: yby
 * @desc:
 * @date: 2024/10/14 15:52
 */
@RestController
@RequestMapping("/tianmu/auth2")
@Slf4j
public class TianMuAuth2Login {

   @RequestMapping(value = {"/login","alogin"})
   public String login(HttpServletRequest request, HttpServletResponse response,
                       @RequestParam(required = false, value = "from", defaultValue = "record") String from) throws IOException {
      String body = getRequestBody(request);

      log.info("from:"+ from);
      log.info("start--------------");
      String token = "";

      // 用户授权：
      //url = "https://{domain}/portal/oauth/authorize?response_type=code&client_id={client_id}&redirect_uri={redirect_uri}&state={state}";
      // https://portal-test.aigccn.net/portal/oauth/authorize?response_type=code&client_id=Q0Yjg5YmYzMTJiMmViZWFhMmIZmIzMjcwOTRjZmM1ND&redirect_uri=https://www.baidu.com&state=params
//      String url = "https://"+domain+"/portal/oauth/authorize?response_type=code&client_id="+client_id+"&redirect_uri="+redirect_uri+"&state="+state;

      String userUrl1 = "https://testi.tmuyun.com/tmy3/tmy3api/authcenter/openapi/v3/getUserInfo"; //
      String userUrl2 = "https://csi.tmuyun.com/tmy3/tmy3api/authcenter/openapi/v3/getUserInfoB"; // 返回503 Service Temporarily Unavailable
      String userUrl3 = "https://mp.tmuyun.com/tmy3/tmy3api/authcenter/openapi/v3/getUserInfoB"; // 返回 "code":"VALIDATION_FAILED","message":"认证失败"

      Map<String, Object> headers3 = new HashMap<>();
      headers3.put("Authorization","cc0bb68855e7c75fe3484973ce900860"); //  cc0bb68855e7c75fe3484973ce900860  KyMQ6OSWG2o4DXrj:1663037682116:yYZmSLJkv9V-F0
      headers3.put("Content-Type","application/json");
      String userInfo = HttpClientUtils.doGetWithTimeout(userUrl1,null, headers3);
      log.info("end-------------:"+userInfo);
      //response.sendRedirect(url);
      return "";
   }

   public static void main(String[] args) {
      String userUrl1 = "https://testi.tmuyun.com/tmy3/tmy3api/authcenter/openapi/v3/getUserInfo";
      Map<String, Object> headers3 = new HashMap<>();
      headers3.put("Authorization","cc0bb68855e7c75fe3484973ce900860"); //  cc0bb68855e7c75fe3484973ce900860  KyMQ6OSWG2o4DXrj:1663037682116:yYZmSLJkv9V-F0
      headers3.put("Content-Type","application/json");
      String userInfo = HttpClientUtils.doGetWithTimeout(userUrl1,null, headers3);
   }

   private String getRequestBody(HttpServletRequest req) {
      StringBuilder stringBuilder = new StringBuilder();
      BufferedReader bufferedReader = null;
      try {
         InputStream inputStream = req.getInputStream();
         if (inputStream != null) {
            bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
            char[] charBuffer = new char[128];
            int bytesRead = -1;
            while ((bytesRead = bufferedReader.read(charBuffer)) > 0) {
               stringBuilder.append(charBuffer, 0, bytesRead);
            }
         } else {
            stringBuilder.append("");
         }
      } catch (IOException ex) {
      } finally {
         if (bufferedReader != null) {
            try {
               bufferedReader.close();
            } catch (IOException ex) {
            }
         }
      }
      return stringBuilder.toString();
   }
}
